package top.lyc829.mymusic.ui.fragment.comment

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chad.library.adapter.base.BaseBinderAdapter
import top.lyc829.mymusic.base.BaseViewModel
import top.lyc829.mymusic.model.bean.Comment
import top.lyc829.mymusic.model.bean.PageInfo
import top.lyc829.mymusic.model.http.MyNetwork
import top.lyc829.mymusic.model.repository.CommentRepository
import top.lyc829.mymusic.model.repository.PlayListRepository

class CommentViewModel : BaseViewModel() {

    private var commentRepository: CommentRepository =
        CommentRepository.getInstance(MyNetwork.getInstance())

    var id:Long = -1

    // 可以直接快速使用，也可以继承BaseBinderAdapter类，重写自己的相关方法
    val adapter: BaseBinderAdapter by lazy { BaseBinderAdapter() }
    val pageInfo: PageInfo = PageInfo()

    private val _hotComments: MutableLiveData<List<Comment>> by lazy { MutableLiveData() }
    val hotComments: LiveData<List<Comment>> get() = _hotComments

    private val _comments: MutableLiveData<List<Comment>> by lazy { MutableLiveData() }
    val comments: LiveData<List<Comment>> get() = _comments


    fun getComments(id: Long) {
        launch(true, {
            val result = commentRepository.getPlayListComments(id, 20,  pageInfo.page * 20)
            if (result.hotComments.isNotEmpty()) {
                _hotComments.value = result.hotComments
            }
            pageInfo.page++
            _comments.value = result.comments
        }, {
            errorLiveData.value = it
        })
    }

}